package com.asap.demo.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 
 * @author wangwei
 *
 */
public final class JsonParser {
	
	private JsonParser(){}
	
	/**
	 * @Title: parseFrom   
	 * @Description: TODO  
	 * @param json json字符串
	 * @param <TValue> TValue
	 * @param <TKey> TKey
	 * @return Map<TKey,TValue> Map<TKey,TValue>  
	 * @throws
	 */
	public static <TKey, TValue> Map<TKey, TValue> parseFrom(String json) {
		
		return (Map<TKey, TValue>)JSON.parseObject(json, 
				new TypeReference<LinkedHashMap<TKey, TValue>>(){}, Feature.OrderedField);
	}
	
	/**
	 * 
	 * @param <TKey> typeOfKey
	 * @param <TValue> typeOfValue
	 * @param map map
	 * @return json
	 */
	public static <TKey, TValue> String toJson(Map<TKey, TValue> map) {
		
		return JSON.toJSONString(map);
	}
}
